package com.lbloca.locate.weight;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.lbloca.locate.R;
import com.lbloca.locate.biz.Config;
import com.lbloca.locate.ui.activity.PrivacyNotesActivity;
import com.lbloca.locate.utils.SPUtils;


public class AgreeDialog extends Dialog implements View.OnClickListener {

    private Activity activity;
    private TextView tvText;

    public AgreeDialog(@NonNull Context context) {
        this(context, R.style.myDialog);
    }

    public AgreeDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.activity = (Activity) context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_agree);

        tvText = findViewById(R.id.tv_text);
        String text = tvText.getText().toString().trim();
        text = String.format(text, activity.getString(R.string.app_name));
        SpannableString ss = new SpannableString(text);
        ss.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Intent intent = new Intent(activity, PrivacyNotesActivity.class);
                intent.putExtra(PrivacyNotesActivity.ARG_TITLE, "用户协议");
                activity.startActivity(intent);
            }
        }, text.lastIndexOf("《用户"), text.lastIndexOf("协议》") + 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Intent intent = new Intent(activity, PrivacyNotesActivity.class);
                intent.putExtra(PrivacyNotesActivity.ARG_TITLE, "隐私政策");
                activity.startActivity(intent);
            }
        }, text.lastIndexOf("《隐私"), text.lastIndexOf("政策》") + 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tvText.setMovementMethod(LinkMovementMethod.getInstance());
        tvText.setHighlightColor(Color.TRANSPARENT);
        ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(activity, R.color.c2a89ff)), text.lastIndexOf("《用户"), text.lastIndexOf("协议》") + 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(activity, R.color.c2a89ff)), text.lastIndexOf("《隐私"), text.lastIndexOf("政策》") + 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tvText.setText(ss);

        findViewById(R.id.tv_yes).setOnClickListener(this);
        findViewById(R.id.tv_no).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_no:
                activity.finish();
                break;
            default:
                SPUtils.getInstance().put(Config.SPF_PRIVACY, true);
        }
        dismiss();
    }
}
